import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    levelList: []
  },
  getters: {
  },
  mutations: {
    setLevel (state, payload) {
      if (payload.title === '工作台') return false
      const i = state.levelList.some(item => {
        return item.title === payload.title
      })
      if (!i) {
        state.levelList.push(payload)
      } else {
        return false
      }
    },
    delLevel (state, title) {
      state.levelList = state.levelList.filter((item) => item.title !== title)
    },
    delLeftLevel (state, title) {
      while (1) {
        if (state.levelList[0].title === title) return true
        state.levelList.shift()
      }
    },
    delRightLevel (state, title) {
      while (1) {
        if (state.levelList[state.levelList.length - 1].title === title) return true
        state.levelList.pop()
      }
    },
    async delOtherLevel (state, title) {
      await this.commit('delLeftLevel', title)
      await this.commit('delRightLevel', title)
    },
    delAllLevel (state) {
      state.levelList = []
    }
  },
  actions: {
  },
  modules: {
  }
})
